/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package common.xmlObjects;

import common.Objects.Component;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *<component>
 *	<id>1</id>
 *	<name>test1</name>
 *	<description>this is the test 1 component</description>
 *	<stock>0</stock>
 *	<price>10</price>
 *	<category>0</category>
 *</component>
 * @author Richard Irons
 */
public class ComponentXML extends XMLNode {
    public static final String ID = "id";
    public static final String NAME = "name";
    public static final String DESCRIPTION = "description";
    public static final String STOCK = "stock";
    public static final String PRICE = "price";
    public static final String CATEGORY = "category";

    private XMLNode id;
    private XMLNode name;
    private XMLNode description;
    private XMLNode stock;
    private XMLNode price;
    private XMLNode category;
    
    public ComponentXML() {
        this(null,null,null,null,null,null);
    }
    public ComponentXML(Component component) {
        this(
            String.valueOf(component.getId()),
            component.getName(),
            component.getDescription(),
            String.valueOf(component.getCurrentStock()),
            String.valueOf(component.getPrice()),
            String.valueOf(component.getCategoryId()));
    }
    public ComponentXML (
            String id,
            String name,
            String description,
            String stock,
            String price,
            String category) {
        
        super(BaseConnectionXML.COMPONENT,null);
        initChildren();
        
        setIdData(id);
        setNameData(name);
        setDescriptionData(description);
        setStockData(stock);
        setPriceData(price);
        setCategoryData(category);

        reAttachChildren();
    }
    public ComponentXML(String xml) {
        this(BaseConnectionXML
            .XMLStrTODOMDoc(xml)
            .getElementsByTagName(
                BaseConnectionXML.COMPONENT)
                .item(0));
    }
    public ComponentXML(Node xmlNode) {
        this ();
        NodeList nodeList = xmlNode.getChildNodes();
        for (int j = 0; j < nodeList.getLength(); j++) {
            Element element = (Element) nodeList.item(j);
            String elementName = element.getTagName();
            String elementValue = element.getTextContent();
            if (elementName.equals(ComponentXML.ID)) {
                this.setIdData(elementValue);
            }
            else if (elementName.equals(ComponentXML.NAME)) {
                this.setNameData(elementValue);
            }
            else if (elementName.equals(ComponentXML.DESCRIPTION)) {
                this.setDescriptionData(elementValue);
            }
            else if (elementName.equals(ComponentXML.STOCK)) {
                this.setStockData(elementValue);
            }
            else if (elementName.equals(ComponentXML.PRICE)) {
                this.setPriceData(elementValue);
            }
            else if (elementName.equals(ComponentXML.CATEGORY)) {
                this.setCategoryData(elementValue);
            }
        }
        reAttachChildren();
    }
    private void initChildren(){
        id = new XMLChildNode(ID,null);
        name = new XMLChildNode(NAME,null);
        description = new XMLChildNode(DESCRIPTION,null);
        stock = new XMLChildNode(STOCK,null);
        price = new XMLChildNode(PRICE,null);
        category = new XMLChildNode(CATEGORY,null);
    }
    public void reAttachChildren() {
        this.removeAllChildNodes();        
        addChildNode(id);
        addChildNode(name);
        addChildNode(description);
        addChildNode(stock);
        addChildNode(price);
        addChildNode(category);
        optimiseChildren();
    }
    public void optimiseChildren() {
        if (id.getNodeData() == null || id.getNodeData().isEmpty() || id.getNodeData().equals("-1")) {
            id.setNodeData("-1");
            removeChildNode(id);
        }
        if (name.getNodeData() == null || name.getNodeData().isEmpty()) {
            name.setNodeData("");
            removeChildNode(name);
        }
        if (description.getNodeData() == null || description.getNodeData().isEmpty()) {
            description.setNodeData("");
            removeChildNode(description);
        }
        if (stock.getNodeData() == null || stock.getNodeData().isEmpty() || stock.getNodeData().equals("-1")) {
            stock.setNodeData("-1");
            removeChildNode(stock);
        }
        if (price.getNodeData() == null || price.getNodeData().isEmpty() ||price.getNodeData().equals("-1")) {
            price.setNodeData("-1");
            removeChildNode(price);
        }
        if (category.getNodeData() == null || category.getNodeData().isEmpty() || category.getNodeData().equals("-1")) {
            category.setNodeData("-1");
            removeChildNode(category);
        }
    }

    public Component getObject() {
        Component component = new Component();
        component.setId(Integer.parseInt(getIdData()));
        component.setName(getNameData());
        component.setDescription(getDescriptionData());
        component.setCurrentStock(Integer.parseInt(getStockData()));
        component.setPrice(Double.parseDouble(getPriceData()));
        component.setCategoryId(Integer.parseInt(getCategoryData()));
        return component;
    }

    public String getCategoryData() {
        return category.getNodeData();
    }

    public void setCategoryData(String category) {
        this.category.setNodeData(category);
        reAttachChildren();
    }

    public String getDescriptionData() {
        return description.getNodeData();
    }

    public void setDescriptionData(String description) {
        this.description.setNodeData(description);
        reAttachChildren();
    }

    public String getIdData() {
        return id.getNodeData();
    }

    public void setIdData(String id) {
       this.id.setNodeData(id);
        reAttachChildren();
    }

    public String getNameData() {
        return name.getNodeData();
    }

    public void setNameData(String name) {
        this.name.setNodeData(name);
        reAttachChildren();
    }

    public String getPriceData() {
        return price.getNodeData();
    }

    public void setPriceData(String price) {
        this.price.setNodeData(price);
        reAttachChildren();
    }

    public String getStockData() {
        return stock.getNodeData();
    }

    public void setStockData(String stock) {
        this.stock.setNodeData(stock);
        reAttachChildren();
    }
}